/*
CPSC 335, Spring Semester 2011, CSU Fullerton
Project 3 Solution

This C++ code implements exhaustive search for the knapsack problem
corresponding to picking an optimal set of Debian packages for a piece
of installation media.

This code was written and tested on a 32 bit Ubuntu Linux machine.  It
uses the clock_gettime function to calculate elapsed times; that
function may not be available on other platforms.  I built this code
with the shell command

  $ g++ proj3.cc deb_knap.cc exhaustive.cc -lrt -o proj3

The -lrt argument is necessary to link librt which contains
clock_gettime.

None of the other source files reference OS-specific features, so they
should be portable to other platforms.

In case it matters, this code is hereby placed under the MIT/X11 License:

Copyright (c) 2011, Kevin A. Wortman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <iostream>
//#include <stdint.h>    // For clock_gettime.
#include <time.h>
#include "exhaustive.h"

using namespace std;

// Run exhaustive search once and print out the result.
void trial(const PackageVector& packages, int n, int W);

//// Convert a timespec into a number of seconds, stored in a double.
//inline double seconds(const struct timespec& t) {
//  return static_cast<double>(t.tv_sec) + static_cast<double>(t.tv_nsec*1E-9);
//}
//
//// Calculate the number of seconds elapsed between timespec objects
//// representing a start and end time.
//inline double elapsed(const struct timespec& start,
//		      const struct timespec& end) {
//  return seconds(end) - seconds(start);
//}

int main() {
  PackageVector* p = loadPackages("packages.txt");
  if (!p) {
    cout << "Error loading packages.txt" << endl;
    return -1;
  }

  trial(*p, 20, 1000);
  trial(*p, 22, 720);
  trial(*p, 22, 1440);
  trial(*p, 22, 25600);
  trial(*p, 22, 65536);

  delete p;

  return 0;
}

void trial(const PackageVector& packages, int n, int W) {
  ///*assert(n > 0);
  //assert(W > 0);

  //struct timespec t1, t2;
  PackageSet* result;

  //cout << "--- n=" << n << " W=" << W << endl;
  //cout << "  -- Exh*/austive search solution --" << endl;
  
  //clock_gettime(CLOCK_REALTIME, &t1);
  long time = clock();
  result = exhaustiveKnapsack(packages, n, W);
  time = clock() - time;
  cout << time << endl;
  cout << result->show(5);
  //clock_gettime(CLOCK_REALTIME, &t2);
  ///*cout << result->show(5);
  //cout << "     Elapse*/d time = " << elapsed(t1, t2) << " seconds" << endl;
  delete result;
}
